<template>
	<view class="content">
		<view class="form-con">
			<view class="cu-form-group">
				<input maxlength="11" placeholder="请输入店铺名称" v-model="shopName" name="input" type="text"></input>
			</view>
			<view class="cu-form-group">
				<input maxlength="11" placeholder="请输入店主姓名" v-model="name" name="input" type="text"></input>
			</view>
			<view class="cu-form-group">
				<input maxlength="11" placeholder="请输入联系电话" v-model="tel" name="input" type="number"></input>
			</view>
			<view class="cu-form-group">
				<input placeholder="请输入验证码" v-model="memo1" name="input"></input>
				<button @tap="getSendMessages" :disabled="codeDisabled" class='cu-btn bg-green shadow'>{{getCodeText}}</button>
			</view>
			<view class="cu-form-group">
				<input maxlength="11" placeholder="请输入店铺地址" v-model="addr" name="input" type="text"></input>
			</view>
			<view class="certifica-uploadimg">
				<view class="title"> ......身份证...... </view>
				<view class="text-center">
					<view class="flex" >
						<view class="solids" @tap="ChooseImage(1)" >
							<image mode="scaleToFill" :src="cardAimg"></image>
							<view class="title1">
								身份证正面
							</view>
						</view>
						<view class="solids" @tap="ChooseImage(2)">
							<image mode="scaleToFill" :src="cardBimg"></image>
							<view class="title1">
								身份证正面
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="certifica-uploadimg">
				<view class="title"> ......身份证...... </view>
				<view class="text-center image-con">
					<view class="flex" >
						<view class="solids" @tap="ChooseImage(1)" >
							<image mode="scaleToFill" :src="cardAimg"></image>
						</view>
						<view class="solids" @tap="ChooseImage(2)">
							<image mode="scaleToFill" :src="cardBimg"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="flex flex-direction login-btn">
				<button class="cu-btn bg-red margin-tb-sm lg" @tap="goReview">提交审核</button>
			</view>
		</view>
	</view>
</template>

<script>
	var sourceType = [
	    ['camera'],
	    ['album'],
	    ['camera', 'album']
	]
	var sizeType = [
	    ['compressed'],
	    ['original'],
	    ['compressed', 'original']
	]
	export default {
		data() {
		return {
			tel:"",//手机号
			getCodeText:'获取验证码',
			codeDisabled:false,//发送验证码是否禁止
			memo1:"",
			shopName:"",
			name:"",
			addr:"",
			checkbox: [{//复选框
				value: 'A',
				checked: false
			}],
			cardAimg:'../../static/images/addPic.png',
			cardBimg:'../../static/images/addPic.png',
			cardCimg:'../../static/images/addPic.png',
			cardDimg:'../../static/images/addPic.png'
			}
		},
		methods: {
			//获取验证码
			getSendMessages(){
				let that=this;
				let tel=this.tel;
				//验证手机号码
			
				if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(tel))){ 
					// that.$api.msg('请填写正确手机号码');
					that.logining = false;
					return false; 
				}
	
				that.getCodeText = "发送中...";
				that.codeDisabled=true;
				sendMessages(tel).then(re =>{
					console.log(re,2)
					setTimeout(()=>{
						that.$api.msg('验证码已发送');
						that.setTimer();
					},1000)
				}).catch(re=>{
					console.log(re,1)
					that.logining=false;
					that.codeDisabled=false;
				});
			},
			//审核
			goReview(){
				let that=this;
				let tel = this.tel;
				let memo1 =this.memo1;
				that.logining=true;
				if(!that.validation(tel,passWord,newpassWord,pUserName,memo1)){
					that.logining=false;
					return false;
				}
				register(memo1,pUserName,passWord,tel,1).then(re =>{
					that.$api.msg('注册成功');
					setTimeout(()=>{
						uni.navigateBack();
					},1000)
				}).catch(re=>{
					that.logining=false;
					that.codeDisabled=false;
				});
				uni.redirectTo({
					url: 'submit_review'
				})
				
			},
			//选择图片
			ChooseImage: async function(type) {
			    // #ifdef APP-PLUS
			    // TODO 选择相机或相册时 需要弹出actionsheet，目前无法获得是相机还是相册，在失败回调中处理
			    if (this.sourceTypeIndex !== 2) {
			        let status = await this.checkPermission();
			        if (status !== 1) {
			            return;
			        }
			    }
			    // #endif
			    uni.chooseImage({
			        sourceType: sourceType[this.sourceTypeIndex],
			        sizeType: sizeType[this.sizeTypeIndex],
			        count: 1,
			        success: (res) => {
						var that=this;
						var tempFilePaths=res.tempFilePaths;
						uni.uploadFile({
							url: config.requestUrl+'/uploadCt/uploadImg',
							filePath: tempFilePaths[0],
							name: 'file',
							method:'POST',
							header:{
								'Content-Type': 'application/x-www-form-urlencoded',
							},
							success: (res) => {
								let json = eval('('+ res.data + ')');
								var imgSrc =json.result.imgSrc;
								if(type==1){
									that.cardAimg=imgSrc;
									console.log(that.cardAimg);
								}else if(type==2){
									that.cardBimg=imgSrc;
									console.log(that.cardBimg);
								}
							},
							fail:(err) => {
								console.log(err)
							}
						})
			        },
			        fail: (err) => {
			            // #ifdef APP-PLUS
			            if (err['code'] && err.code !== 0 && this.sourceTypeIndex === 2) {
			                this.checkPermission(err.code);
			            }
			            // #endif
			        }
			    })
			},
			async checkPermission(code) {
			    let type = code ? code - 1 : this.sourceTypeIndex;
			    let status = permision.isIOS ? await permision.requestIOS(sourceType[type][0]) :
			        await permision.requestAndroid(type === 0 ? 'android.permission.CAMERA' :
			            'android.permission.READ_EXTERNAL_STORAGE');
			
			    if (status === null || status === 1) {
			        status = 1;
			    } else {
			        uni.showModal({
			            content: "没有开启权限",
			            confirmText: "设置",
			            success: function(res) {
			                if (res.confirm) {
			                    permision.gotoAppSetting();
			                }
			            }
			        })
			    }
			
			    return status;
			},
			//短信倒计时
			setTimer(){
				let holdTime = 60;
				this.getCodeText = "重新获取(60)"
				this.Timer = setInterval(()=>{
					if(holdTime<=0){
						this.getCodeBtnColor = "#ffffff";
						this.getCodeText = "获取验证码";
						this.codeDisabled=false;
						clearInterval(this.Timer);
						return ;
					}
					this.getCodeText = "重新获取("+holdTime+")"
					holdTime--;
					
				},1000)
			},
			//验证
			validation(tel,passWord,newpassWord,pUserName,memo1) {
				var pictureCode =this.pictureCode;
				if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(tel))){
					this.$api.msg('请输入正确手机号码');
					return false; 
				}
				if(passWord===''){
					this.$api.msg('请输入密码');
					return false; 
				}
				if(newpassWord===''){
					this.$api.msg('请确认密码');
					return false; 
				}
				
				if(pUserName===''){
					this.$api.msg('请输入推荐人账号');
					return false; 
				}
				if(memo1===''){
					this.$api.msg('请输入验证码');
					return false; 
				}
				if(passWord!==newpassWord){
					this.$api.msg('两次密码输入不一致');
					return false; 
				}
				return true;
			},
			CheckboxChange() {//复选框切换选中
				this.checkbox[0].checked = !this.checkbox[0].checked
			},
		}
	}
</script>

<style>
	page{background: #ffffff!important;}
	/* .content{padding:0 56rpx;} */
	.form-con{width:100%;/* margin-top:44rpx; */}
	.content{display: flex;flex-direction: column;align-items: center;}
	.cu-form-group{border-bottom: 1px solid #efefef;border-top:0!important}
	/* .login-btn{margin-top:172rpx;} */
	.login-btn button{
		border-radius: 58rpx!important;
		background: url("../../static/images/band-card.png") no-repeat;
		background-size: 100% 100%;
		color: #fff;	
		width:600rpx;
		height:80rpx;
		margin:46rpx auto;
		line-height: 80rpx;
		text-align: center;
		font-size: 28rpx;}
	.text{margin-top:20rpx;}
	.text text{color: #FF9800;}
	.bg-green{background: none;box-shadow: none;border:0;color: #FF5454;height:60rpx;}
	.certifica-uploadimg .title{color: #101010;font-size: 28rpx;padding:60rpx;text-align: center;}
	.solids{border-radius: 10rpx;height:200rpx;width:300rpx;margin:0 36rpx 16rpx;}
	.solids image{border-radius: 10rpx;height:200rpx;width:300rpx;}
	.title1{font-size: 24rpx;color: #666666;margin-top:16rpx;}
</style>
